//
//  QSCExpertInterrogationCollectionCell.m
//  EasyHealth
//
//  Created by lifuzhou on 2017/3/17.
//  Copyright © 2017年 qingsongchou. All rights reserved.
//

#import "SHExpertInterrogationCollectionCell.h"
#import "SHTestCollectionViewCell.h"
#import "SHImage.h"

#define kDeviceWidth [UIScreen mainScreen].bounds.size.width

static NSString *const kQSCAddImageCollectionCell = @"SHTestCollectionViewCell";
 
@interface SHExpertInterrogationCollectionCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIScrollViewDelegate>

{
    SHImage        *_lastImage;
     NSMutableArray *_lastArray;
    
    UIPageControl *_pageControl;
    
 }
@property (nonatomic, strong) NSMutableArray *imageArray;

 /// 选中照片数组
@property (nonatomic) NSArray *images;
/// 选中资源素材数组，用于定位已经选择的照片
@property (nonatomic) NSMutableArray *selectedAssets;


@property (nonatomic, copy) NSString *doctorId;
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;


@end

@implementation SHExpertInterrogationCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self buildCollectionView];

}



-(void)buildCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; // 垂直滚动
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    // 注册cell(可以同时注册多个不同的cell)
[_myCollectionView registerNib:[UINib nibWithNibName:@"SHTestCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kQSCAddImageCollectionCell];
 
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.frame = CGRectMake(210, 45, 20, 20);//指定位置大小
    _pageControl.numberOfPages = 2;//指定页面个数
    _pageControl.currentPage = 0;//指定pagecontroll的值，默认选中的小白点（第一个）
    //添加委托方法，当点击小白点就执行此方法
    
    _pageControl.pageIndicatorTintColor = [UIColor redColor];// 设置非选中页的圆点颜色
    
    _pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 设置选中页的圆点颜色
    [self addSubview:_pageControl];
    
  
    self.imageArray = [[NSMutableArray alloc] init];
    _lastImage = [[SHImage alloc] init];
    _lastImage.imageV = [UIImage imageNamed:@"askdoctor_addphoto"];
    _lastImage.cellType = cellTypeAdd;
    [self.imageArray addObject:_lastImage];
}

#pragma mark -- UICollectionDelegate --
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    SHTestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kQSCAddImageCollectionCell forIndexPath:indexPath];
    if (_imageArray.count > indexPath.row) {
        SHImage *model = _imageArray[indexPath.row];
        cell.celltype = model.cellType;
    }
    return cell;
}

//定义每个UICollectionViewcell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kDeviceWidth - 60) / 3.0 - 0.5, 45);
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}

//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
// 点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHImage *model = [self.imageArray lastObject];;
    if (model.cellType == cellTypeAdd) {
        [self.imageArray removeLastObject];
        SHImage *cellmodel = [[SHImage alloc] init];
        cellmodel.imageV = [UIImage imageNamed:@"pd_pic_add"];
        cellmodel.cellType = cellTypeNormal;
        [self.imageArray addObject:cellmodel];
        
        [self.imageArray  addObject:_lastImage];
        [self.myCollectionView reloadData];
    }else{
        NSLog(@"跳转");
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = self.myCollectionView.contentOffset.x /( self.myCollectionView.frame.size.width/2);
    //    NSLog(@"%d", page);
    
    // 设置页码
    _pageControl.currentPage = page;
}



// header 和 footer
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    
    return nil;
}



@end
